﻿@inherits AdminCompontentBase
<div @ref="Ref">
    <DefaultSheetDialog Value="@_visible" ValueChanged="HandleVisibleChanged" Title="@T("Permission.SendTemplateMessage")">
        <ChildContent>
            <MForm Model="_model" EnableValidation EnableI18n @ref="_form" Class="full-height mt-n2 pt-3">
                <MStepper Value="_model.Step" Elevation=0 Class="d-flex flex-column full-height">
                    <MStepperHeader Class="ma-n6" Style="box-shadow:none !important">
                        <MStepperStep Step="1" Complete="_model.Step>1">
                            @T("DisplayName.MessageInfoContent")
                        </MStepperStep>
                        <MDivider></MDivider>
                        <MStepperStep Step="2" Complete="_model.Step>2">
                            @T("DisplayName.MessageTaskReceiver")
                        </MStepperStep>
                        <MDivider></MDivider>
                        <MStepperStep Step="3">
                            @T("DisplayName.MessageTaskSendingRule")
                        </MStepperStep>
                    </MStepperHeader>
                    <MStepperItems Class="flex-grow-1">
                        <MStepperContent Class="full-height pa-0" Step="1">
                            <AutoHeight Overflow>
                                <HeaderContent>
                                    <div class="mb-6"></div>
                                </HeaderContent>
                                <AutoHeightContent>
                                    <MRow Class="mt-0">
                                        <MCol>
                                            <SSelect @bind-Value="@_model.ChannelType"
                                                     Items="@(GetEnumList<ChannelTypes>().ToList())"
                                                     Label="@T("DisplayName.ChannelType")"
                                                     ItemText="item => T(item.ToString())"
                                                     ItemValue="item => item"
                                                     TItem="ChannelTypes"
                                                     TItemValue="ChannelTypes"
                                                     TValue="ChannelTypes?"
                                                     OnSelectedItemUpdate="HandleChannelTypeChangeAsync">
                                            </SSelect>
                                        </MCol>
                                    </MRow>
                                    <MRow Class="mt-6">
                                        <MCol>
                                            <SSelect @bind-Value="_model.ChannelId"
                                                     Items="@_channelItems"
                                                     Label="@T("DisplayName.Channel")"
                                                     ItemText="item => item.DisplayName"
                                                     ItemValue="item => item.Id"
                                                     TItem="ChannelDto"
                                                     TItemValue="Guid"
                                                     TValue="Guid?"
                                                     OnSelectedItemUpdate="HandleChannelChangeAsync">
                                            </SSelect>
                                        </MCol>
                                    </MRow>
                                    <MRow Class="mt-6">
                                        <MCol>
                                            <SAutoComplete Items="_templateItems" @bind-Value="_model.EntityId"
                                                           Label="@T("TemplateTemplate")"
                                                           ItemText="@(x=>string.IsNullOrEmpty(x.TemplateId)?x.DisplayName:$"{x.DisplayName}({x.TemplateId})")"
                                                           ItemValue="x=>x.Id"
                                                           Dense
                                                           Outlined
                                                           HideDetails="@("auto")"
                                                           TItem="MessageTemplateDto"
                                                           TItemValue="Guid"
                                                           TValue="Guid"
                                                           OnSelectedItemUpdate="HandleTemplateSelectedAsync"></SAutoComplete>
                                        </MCol>
                                    </MRow>
                                    @if (_model.EntityId != default)
                                    {
                                        <TemplateInfo Template="_messageInfo"></TemplateInfo>
                                        
                                        @if (_model.Variables.Any())
                                        {
                                            <MessageVariables Class="mt-9" @bind-Value="_model.Variables"></MessageVariables>
                                        }
                                    }
                                </AutoHeightContent>
                                <FooterContent>
                                    <div class="d-flex mt-6">
                                        <MSpacer></MSpacer>
                                        <SButton Medium OnClick="async() => await Throttle(HandleSaveAsync)" Outlined>@T("Save")</SButton>
                                        <SButton Medium OnClick="()=> HandleNextStep()" Class="ml-6">@T("NextStep")</SButton>
                                    </div>
                                </FooterContent>
                            </AutoHeight>
                        </MStepperContent>
                        <MStepperContent Class="full-height pa-0" Step="2">
                            <AutoHeight Overflow>
                                <AutoHeightContent>
                                    @if (_model.ComputedBroadcast && !_selectReceiverType)
                                    {
                                        <ReceiverTypeSelect @bind-value="_model.ReceiverType" OnClick="HandleReceiverType" />
                                    }
                                    else
                                    {
                                        <MInput @bind-Value="_model.Receivers" />
                                        <MessageReceivers @bind-ReceiverType="_model.ReceiverType"
                                                      @bind-SelectReceivers="_selectReceivers"
                                                      @bind-ImportReceivers="_importReceivers"
                                                      @bind-SelectReceiverType="_model.SelectReceiverType"
                                                      ChannelType="_model.ChannelType"
                                                      MessageTemplatesId="_model.EntityId" />
                                    }
                                </AutoHeightContent>
                                <FooterContent>
                                    <div class="d-flex mt-6">
                                        <MButton Icon OnClick="HandleReceiverBack">
                                            <SIcon Size="24" Color="emphasis2">mdi-keyboard-backspace</SIcon>
                                        </MButton>
                                        <MSpacer />
                                        @if (!_model.ComputedBroadcast || _selectReceiverType)
                                        {
                                            <SButton Medium OnClick="async() => await Throttle(HandleSaveAsync)" Outlined>@T("Save")</SButton>
                                            <SButton Medium Class="ml-6" OnClick="()=> HandleNextStep()">@T("NextStep")</SButton>
                                        }
                                    </div>
                                </FooterContent>
                            </AutoHeight>
                        </MStepperContent>
                        <MStepperContent Class="full-height pa-0" Step="3">
                            <AutoHeight Overflow>
                                <AutoHeightContent>
                                    <MInput @bind-Value="_model.SendRules" />
                                    @if (_messageInfo.Channel != null && _messageInfo.Channel.Type == ChannelTypes.Sms)
                                    {
                                        <MRow Class="mt-6">
                                            <MCol>
                                                <STextField @bind-Value="_model.Sign"
                                                        Label="@T("DisplayName.MessageTemplateSign")">
                                                </STextField>
                                            </MCol>
                                        </MRow>
                                    }
                                    <MessageSendingRules Class="mt-2" @bind-Value="_model.SendRules" />
                                </AutoHeightContent>
                                <FooterContent>
                                    <div class="d-flex mt-6">
                                        <MButton Icon OnClick="HandleSendingRuleBack">
                                            <SIcon Size="24" Color="emphasis2">mdi-keyboard-backspace</SIcon>
                                        </MButton>
                                        <MSpacer />
                                        <SButton Medium OnClick="async() => await Throttle(HandleSaveAsync)" Outlined>@T("Save")</SButton>
                                        <SButton Medium Class="ml-6" OnClick="async() => await Throttle(HandleSendAsync)">@T("SaveSend")</SButton>
                                    </div>
                                </FooterContent>
                            </AutoHeight>
                        </MStepperContent>
                    </MStepperItems>
                </MStepper>
            </MForm>
        </ChildContent>
    </DefaultSheetDialog>
</div>